<?php

namespace App\Http\Controllers\student;

use App\Model\WorkStudent;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Model\TheClass;
use App\Model\ClassStudent;
use App\Model\Message;
use App\Model\Work;
use App\Model\Attendance;
use App\Model\AttendanceStudent;

class ClassController extends Controller
{
    private $classModel;
    public function __construct(TheClass $classModel)
    {
        $this->classModel = $classModel;
    }

    public function index($classId)
    {

        $studentId = Auth::guard('student')->id();
        $messageModel = new Message();
        $messages = $messageModel->where('student_id', $studentId)->where('class_id', $classId)->orderBy('created_at')->get();



        return view('student.class', compact('messages', 'classId'));
    }

    public function addClass(Request $request)
    {
        $studentId = Auth::guard('student')->id();
        $invitationCode = $request->input('invitation_code');
        $classModel = new TheClass();
        $class = $classModel->where('invitation_code', $invitationCode)->first();
        if (!$class) {
            return $this->apiResponse(404, '邀请码错误！');
        }

        $classStudentModel = new ClassStudent();
        $isExist = $classStudentModel->where('student_id', $studentId)->where('class_id', $class->id)->first();
        if ($isExist) {
            return $this->apiResponse(403, '已经加入该课程，不可重复加入');
        }
        $classStudent = $classStudentModel->create(['class_id' => $class->id, 'student_id' => $studentId]);
        if (!$classStudent) {
            return $this->apiResponse(500, '服务器错误，请稍后重试');
        }
        return $this->apiResponse(200, '成功加入课程！');



    }

    public function exitClass(Request $request)
    {
        $studentId = Auth::guard('student')->id();
        $classId = $request->input('class_id');
        $classStudentModel = new ClassStudent();
        $classStudent = $classStudentModel->where('class_id', $classId)->where('student_id', $studentId)->first();

        if (!$classStudent) {
            return $this->apiResponse(404, '未加入该课程，请刷新页面后重试');
        }

        $result = $classStudent->delete();

        if (!$result) {
            return $this->apiResponse(500, '服务器错误，请稍后重试');
        }

        return $this->apiResponse(200, '退出成功');


    }

    public function sendMessage(Request $request, $class_id)
    {
        //$classId = $request->input('class_id');
        $content = $request->input('content');
        $studentId = Auth::guard('student')->id();
        $messageModel = new Message();

        $message = $messageModel->create(['student_id' => $studentId, 'class_id' => $class_id, 'teacher_id' => 1, 'content' => $content]);

        if (!$message) {
            return $this->apiResponse(500, '服务器错误，请稍后重试');
        }
        return $this->apiResponse(200, '发送成功');
    }

    public function workPage($classId)
    {
        $studentId = Auth::guard('student')->id();
        $workModel = new work();

        $workList = $workModel->where('class_id', $classId)->orderBy('created_at', 'desc')->get();
        //$students = $workList->get('id');

        foreach ($workList as $k => $work) {
            $res = WorkStudent::where('work_id', $work->id)->where('student_id', $studentId)->first();
            if ($res) {
                $workList[$k]->isCompleted = true;
                continue;
            }
            $workList[$k]->isCompleted = false;
        }

        return view('student.work', compact('classId', 'workList'));
    }

    public function viewWork(Request $request)
    {
        $studentId = Auth::guard('student')->id();
        $work = WorkStudent::where('student_id', $studentId)->where('work_id', $request->input('work_id'))->first();
        $content = $work->content;
        return $this->apiResponse(200, $content);

    }

    public function uploadWork(Request $request)
    {
        $studentId = Auth::guard('student')->id();
        $work = WorkStudent::create([
            'work_id' => $request->input('work_id'),
            'content' => $request->input('content'),
            'student_id' => $studentId
        ]);
        if ($work) {
            return $this->apiResponse(200, '上传成功');
        }
        return $this->apiResponse(403, '上传失败');

    }

    public function attendancePage($classId)
    {
        $studentId = Auth::guard('student')->id();
        $attendanceModel = new Attendance();

        $attendance = $attendanceModel->where('class_id', $classId)->where('status', 0)->first();
        $attendanceStatus = false;
        $attendanceId = 0;
        if ($attendance) {
            $attendanceStudentModel = new AttendanceStudent();
            $isHere = $attendanceStudentModel->where('student_id', $studentId)->where('attendance_id', $attendance->id)->first();
            if (!$isHere) {
                $attendanceStatus = true;
            }
            $attendanceId = $attendance->id;
        }
        return view('student.attendance', compact('classId', 'attendanceStatus', 'attendanceId'));
    }

    public function verifyCode(Request $request)
    {
        $studentId = Auth::guard('student')->id();

        $attendanceId = $request->input('attendance_id');
        $code = $request->input('code');
        $classId = $request->input('class_id');

        $attendanceModel = new Attendance();
        $attendance = $attendanceModel->where('class_id', $classId)->where('status', 0)->first();

        if (!$attendance) {
            return $this->apiResponse(403, '考勤时间已过');
        }

        if ($attendance->code !== (int)$code) {
            return $this->apiResponse(403, '签到码不正确');
        }

        $attendanceStudentModel = new AttendanceStudent();

        $result = $attendanceStudentModel->create(['attendance_id' => $attendanceId, 'student_id' => $studentId, 'status' => 0]);

        if ($result) {
            return $this->apiResponse(200, '签到成功');
        }else {
            return $this->apiResponse(500, '服务器错误，请稍后重试');
        }
    }




}
